package day_2022_9_to_12.Day;

/**
 * @author haomin
 * @date 2022/09/22 22:27
 **/
public class Day20_5_Num111_MinDepth {
    class Solution {
        int minDe = Integer.MAX_VALUE;
        public int minDepth(TreeNode root) {
            if(root == null) return 0;
            dfs(root, 1);
            return minDe;
        }
        private void dfs(TreeNode root, int depth){
            if(root.left == null && root.right == null){
                minDe = Math.min(minDe, depth);
                return;
            }
            if(root.left != null)dfs(root.left, depth+1);
            if(root.right != null)dfs(root.right, depth+1);
            return;
        }
    }
    class Solution2 {
        public int minDepth(TreeNode root) {
            if(root == null) return 0;
            if(root.left == null && root.right != null){
                return 1 + minDepth(root.right);
            }
            if(root.left != null && root.right == null){
                return 1 + minDepth(root.left);
            }
            return 1 + Math.min(minDepth(root.left), minDepth(root.right));
        }
    }
}